﻿using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using ExcelSheetOperateion.NPOI.Extend;
using PrjAssessModels.BaseModels;


namespace ExcelSheetOperateion
{
    /// <summary>
    /// 读取固定格式Excel文件,将文件的内容转化为已定义好的类:Project
    /// </summary>
    public class ReadAssessSheet
    {
        #region Protected Variant
        protected ISheet sheet;
        protected Project prj_infor = null;
        #endregion
        #region Core Method
        public Project ReadWorkBook(string path)
        {
           
            InitialSheet(path);
            if (sheet == null) return null;
            //读取基本信息
            prj_infor = ReadBasicInfo(path);
            //读取材料埋设数据
            prj_infor.MaterialBurys = ReadMaterialBuryData(path);
            prj_infor.Works = ReadWorkData(path);
            prj_infor.FromExcel = true;
            return prj_infor;
        }
        #endregion
        #region Protected Methods
        //初始化Sheet
        protected void InitialSheet(string path)
        {
            sheet = HSSFSheetExtend.OpenSheet(path);
        }
        //读取基本信息
        protected Project ReadBasicInfo(string path)
        {
            prj_infor = new Project();
            ICell temp = this.sheet.FindCell("工程名称");
            prj_infor.PrjName = temp.OffSet(0, 1).GetCellValue();

            temp = this.sheet.FindCell("测算日期",false);
            prj_infor.AssessDate = temp.OffSet(0, 1).DateCellValue;

            temp = this.sheet.FindCell("工程编号");
            prj_infor.PrjId = temp.OffSet(0, 1).GetCellValue().Replace("-", "-").Replace("－", "-").Replace("－", "-");

            temp = this.sheet.FindCell("合同额");
            prj_infor.Contract = temp.OffSet(0, 1).GetCellDouble();

            temp = this.sheet.FindCell("地址");
            prj_infor.Address = temp.OffSet(0, 1).GetCellValue();

            temp = this.sheet.FindCell("项目经理");
            prj_infor.Manager = temp.OffSet(0, 1).GetCellValue();

            temp = this.sheet.FindCell("班组长");
            prj_infor.Leader = temp.OffSet(0, 1).GetCellValue();

            temp = this.sheet.FindCell("采购厂商");
            prj_infor.Manufacture = temp.OffSet(0, 1).GetCellValue();

            temp = this.sheet.FindCell("监测类型");
            prj_infor.MonitorType = temp.OffSet(0, 1).GetCellValue();

            temp = this.sheet.FindCell("工期");
            prj_infor.ConstructionPeriod = (int)temp.OffSet(0, 1).GetCellDouble();

            temp = this.sheet.FindCell("支撑层数", false);
            prj_infor.PrjExtent.SupportNo = (int)temp.OffSet(1, 0).GetCellDouble();

            temp = this.sheet.FindCell("环境复杂性");
            prj_infor.PrjExtent.EnviComplicate = temp.OffSet(1, 0).GetCellDouble();

            temp = this.sheet.FindCell("施作方式");
            prj_infor.PrjExtent.OperateMethod = temp.OffSet(1, 0).GetCellDouble();


            //temp = this.sheet.FindCell("基坑工作量系数", false);
            //prj_infor.PrjExtent.WorkRatio = temp.OffSet(0, 4).GetCellDouble();

            //temp = this.sheet.FindCell("工程综合系数");
            //prj_infor.PrjExtent.PrjRatio = temp.OffSet(0, 1).GetCellDouble();

            temp = this.sheet.FindCell("地域系数");
            prj_infor.PrjExtent.RegionRatio = temp.OffSet(1, 0).GetCellDouble();

            temp = this.sheet.FindCell("复杂度系数");
            prj_infor.PrjExtent.ComplicateRatio = temp.OffSet(1, 0).GetCellDouble();

            temp = this.sheet.FindCell("监测类型系数");
            prj_infor.PrjExtent.SurveyTypeRatio = temp.OffSet(1, 0).GetCellDouble();

            temp = this.sheet.FindCell("班组折减率");
            prj_infor.PrjExtent.TeamRatio = temp.OffSet(1, 0).GetCellDouble();


            temp = this.sheet.FindCell("规模效应折减");
            prj_infor.PrjExtent.ScaleRatio = temp.OffSet(1, 0).GetCellDouble();

            temp = this.sheet.FindCell("材料采购系数");
            prj_infor.PrjExtent.MaterialPurchaseRatio = temp.OffSet(0, 1).GetCellDouble();

            // prj_infor.SurveyNo = prj_infor.PrjExtent.SurveyTypeRatio == 1 ? 40 * (prj_infor.PrjExtent.WorkRatio + 1) : prj_infor.ConstructionPeriod;
         
            prj_infor.PrjExtent.PrjId = prj_infor.PrjId;

            return prj_infor;
        }
        //读取材料埋设数据
        protected List<MaterialBuryItem> ReadMaterialBuryData(string path)
        {
            List<MaterialBuryItem> contents = new List<MaterialBuryItem>();
            ICell temp = sheet.FindCell("材料费").OffSet(2, -2);
            string content_name = "";
            while (temp.OffSet(0, 3).CellType != CellType.BLANK)
            {
                if (temp.StringCellValue != "")
                {
                    content_name = temp.StringCellValue;
                }
                double count = temp.OffSet(0, 2).GetCellDouble();
                //假如有数据
                if (count > 0)
                {
                    MaterialBuryItem mb_item = new MaterialBuryItem();
                    mb_item.PrjId = this.prj_infor.PrjId;
                    mb_item.Number = (int)count;
                    mb_item.ContentName = content_name;
                    mb_item.Method = temp.OffSet(0, 1).GetCellValue();
                    mb_item.MaterialPrice = temp.OffSet(0, 3).GetCellDouble();
                    //mb_item.MaterialTotal = temp.OffSet(0, 4).GetCellDouble();
                    mb_item.BuryPrice = temp.OffSet(0, 6).GetCellDouble();
                    //mb_item.BuryTotal = temp.OffSet(0, 7).GetCellDouble();
                    double para = temp.OffSet(0, 8).GetCellDouble();
                    if (para > 0)
                    {
                        mb_item.Para = para;
                    }
                    contents.Add(mb_item);
                }
                temp = temp.OffSet(1, 0);
            }
            //prj_infor.MaterialSum = temp.OffSet(1, 4).GetCellDouble();
            //prj_infor.BurySum = temp.OffSet(0, 7).GetCellDouble();
            //prj_infor.MaterialBurySum = temp.OffSet(2, 7).GetCellDouble();

            return contents;
        }

        //读取工作量数据
        protected List<WorkItem> ReadWorkData(string path)
        {
            List<WorkItem> works = new List<WorkItem>();
            ICell temp = this.sheet.FindCell("预估次数").OffSet(1, 0);
            while (temp.GetCellDouble() > 0)
            {
                int w_number = (int)temp.OffSet(0, -3).GetCellDouble();
                if (temp.OffSet(0, -5).StringCellValue == "围护体压顶梁")
                {
                    prj_infor.SurveyNo = temp.GetCellDouble();
                }
                if (w_number > 0)
                {
                    WorkItem w_item = new WorkItem();
                    w_item.Number = w_number;
                    w_item.PrjId = prj_infor.PrjId;
                    w_item.Price = temp.OffSet(0, -2).GetCellDouble();
                    //w_item.PriceTotal = temp.OffSet(0, -1).GetCellDouble();
                    w_item.PredictNo = temp.GetCellDouble();
                   // w_item.Sum = temp.OffSet(0, 1).GetCellDouble();
                    w_item.ContentName = temp.OffSet(0, -5).StringCellValue;
                 
                    works.Add(w_item);
                }
                temp = temp.OffSet(1, 0);
            }
            //prj_infor.WorkSum = temp.OffSet(1, 1).GetCellDouble();
            return works;
        }
        #endregion

    }
}
